﻿Imports Automation.BDaq
Imports System.ComponentModel
Public Class BufferAI

    'Public Sub New(bufferAi As WaveformAiCtrl)
    '    Initialized = bufferAi.Initialized
    'End Sub

    '<DescriptionAttribute("设备初始化是否成功")> _
    'Public Property Initialized As Boolean

    ''' <summary>
    ''' 启动Ai板卡
    ''' </summary>
    ''' <param name="bufferAi">被启动的WaveformAiCtrl控件</param>
    ''' <returns>启动状态返回ErrorCode，成功则为ErrorCode.Success</returns>
    ''' <remarks></remarks>
    Public Shared Function InitAi(ByRef bufferAi As WaveformAiCtrl) As ErrorCode
        Dim ret As ErrorCode
        If bufferAi.Initialized Then
            ret = bufferAi.Prepare()
            If ret = ErrorCode.Success Then
                ret = bufferAi.Start()
                If ret = ErrorCode.Success Then
                    Debug.Print("Start Success.")
                    Return ret
                Else
                    Return ret
                    Debug.Print("Start Failed.")
                End If
            Else
                Debug.Print("Prepare Failed.")
                Return ret
            End If
        Else
            MsgBox("Initialize Failed.")
            '暂定这个ErrorCode
            Return ErrorCode.ErrorDeviceNotExist
        End If
    End Function

    ''' <summary>
    ''' 获取一组缓存数据
    ''' </summary>
    ''' <param name="bufferAi">WaveformAiCtril控件，采样率、数据长度等信息在这里设置</param>
    ''' <param name="refBuffer">采样的结果</param>
    ''' <returns>成功则为ErrorCode.Success</returns>
    ''' <remarks></remarks>
    Public Shared Function GetBufferData(ByRef bufferAi As WaveformAiCtrl, ByRef refBuffer As Double()) As ErrorCode
        Dim ret As ErrorCode = ErrorCode.ErrorDeviceNotExist
        Dim [return] As Integer
        If bufferAi.Initialized Then
            ReDim refBuffer(bufferAi.Record.SectionLength * bufferAi.Conversion.ChannelCount - 1)
            Try
                ret = bufferAi.GetData(bufferAi.Record.SectionLength * bufferAi.Conversion.ChannelCount, refBuffer, -1, [return])
                If bufferAi.State = ControlState.Stopped Then
                    bufferAi.Start()
                End If
            Catch ex As Exception
                Debug.Print(ex.ToString)
            End Try
        Else
            Debug.Print("Device Not Initialized.")
        End If
        Return ret
    End Function

    ''' <summary>
    ''' 从连在一起的缓存数据区提取出每个通道的数据
    ''' 
    ''' </summary>
    ''' <param name="bufferAi">从那个waveform提取数据</param>
    ''' <param name="bufferData">连在一起的采样原始数据</param>
    ''' <param name="Channel">通道编号</param>
    ''' <param name="ChannelData">分离后存入的通道数据，经过变比换算</param>
    ''' <param name="ratio">数据变比，默认为1</param>
    ''' <param name="offset">直流偏移量，默认为0</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetChannelData(ByVal bufferAi As WaveformAiCtrl, ByVal bufferData As Double(), ByVal Channel As Integer, ByRef ChannelData As Double(), Optional ByVal ratio As Double = 1.0, Optional ByVal offset As Double = 0) As Boolean
        If bufferAi.Initialized Then
            If bufferData IsNot Nothing Then
                ReDim ChannelData(bufferAi.Record.SectionLength - 1)
                For i As Integer = 0 To UBound(ChannelData)
                    ChannelData(i) = (bufferData(Channel + i * bufferAi.Conversion.ChannelCount) + offset) * ratio
                Next
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 释放控件资源
    ''' </summary>
    ''' <param name="bufferAi"></param>
    ''' <remarks></remarks>
    Public Shared Sub Release(ByRef bufferAi As WaveformAiCtrl)
        Try
            bufferAi.Release()
        Catch ex As Exception
            Debug.Print(ex.ToString)
        End Try
    End Sub

End Class
